<?php

/*
 * This function create new playlist for this $musics data objects array
 * and return path to the playlist file in tmp folder
 * flag_use = 0: for high quality
 * flag_use = 1: for demo
 * flag_use = 2: for sound_trackmix
 * flag_use = -1: not use anything
 */
function create_playlist($musics, $flag_use = -1)
{
	// build file name
	if (count($musics)==0) return "";
	$music_count=0;
	$file_name =exponent_sessions_getTicketString();
	
	$ret_st='<?xml version="1.0" encoding="UTF-8"?><playlist version="1" xmlns="http://xspf.org/ns/0/"><trackList>';
	for ($i=0;$i<count($musics);$i++)
	{
		$location="";
		// get best file
		if (($musics[$i]->high_quality !='') && (($flag_use == 0) || ($flag_use == -1)))
       $location="<location>".BASE_URL."/{$musics[$i]->high_quality}</location>";
		// 9/9/2009: immanuel192 commented this code to fix error: no musics file id in music record but it recognise as music file
    /*else
		if ($musics[$i]->demo !=''  && ((($flag_use == -1) && ($musics[$i]->high_quality =="")) || 
		    ($flag_use == 1)))
        	$location="<location>{$musics[$i]->demo}</location>";
		else
		if ($musics[$i]->sound_trackmix !='' 
		&& ((($flag_use == -1) && ($musics[$i]->high_quality =="") && ($musics[$i]->demo =="")) || 
		    ($flag_use == 2)))
        	$location="<location>{$musics[$i]->sound_trackmix}</location>";*/
		
		//$ret_st.="<creator>{$musics[$i]->composer_name}</creator>";
		if ($location !="")
		{
			$ret_st.="<track>";		
			$ret_st.="<title>{$musics[$i]->name}</title>";
			$ret_st.=$location;
			$ret_st.="</track>";
			$music_count++;
		}
        
        //	$ret_st.="<info>". strip_tags($musics[$i]->old_info) ."</info>";
		//$ret_st.="<image>{$musics[$i]->album_image}</image>";
		
		//
		//$file_name.="{$musics[$i]->id}";
	}
	$file_name.=".xspf";
	$ret_st.="</trackList></playlist>";
	
	// test file
	//if (file_exists(BASE."tmp/playlist/".$file_name))
	//
  if ($music_count > 0)
  {
		$fp=fopen(BASE."tmp/playlist/". $file_name,"w");
		fwrite($fp,$ret_st);
		fclose($fp);
		//chmod($file_name,755);
		return $file_name;
	}
	else return "";
	
}
?>
